package com.pmcc.cj.service;

import com.pmcc.cj.entity.CjCarousel;
import com.pmcc.cj.repository.CjCarouselRepository;
import com.pmcc.utils.AntdPage;
import com.pmcc.utils.BeanUtils;
import com.pmcc.utils.ResultBean;
import com.pmcc.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by tomas on 2020/10/4.
 */
@Transactional
@Service
public class CjCarouselService {
    @Autowired
    CjCarouselRepository cjCarouselRepository;

    public ResultBean addAnnouncement(CjCarousel model){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            CjCarousel advertisement = cjCarouselRepository.findFirstByCarouselTypeOrderBySequenceDesc(model.getCarouselType());
            if(advertisement != null){
                model.setSequence(advertisement.getSequence()+1);
            }else {
                model.setSequence(1);
            }
            model.setDelFlag(0);
            model.setCreateTime(new Date());
            cjCarouselRepository.saveAndFlush(model);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    public ResultBean queryAnnouncementByPage(CjCarousel model, AntdPage antdPage){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            Page page=cjCarouselRepository.findAll(this.getSpecification(model),antdPage.getPageRequest());
            resultBean.setPage(antdPage.getDefaultCurrent());
            resultBean.setTotal(page.getTotalElements());
            resultBean.setPageSize(antdPage.getPageSize());
            resultBean.setRows(page.getContent());
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    public Specification<CjCarousel> getSpecification(CjCarousel model) {
        return new Specification<CjCarousel>() {
            @Override
            public Predicate toPredicate(Root<CjCarousel> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> list = new ArrayList<Predicate>();
                if(model.getCarouselType() != null){
                    list.add(builder.equal(root.get("carouselType").as(Integer.class),  model.getCarouselType()));
                }
                if(StringUtil.isNotEmpty(model.getProductTitle())){
                    list.add(builder.like(root.get("productTitle").as(String.class),  "%"+model.getProductTitle()+"%"));
                }
                list.add(builder.equal(root.get("delFlag").as(Integer.class), 0));
//                query.orderBy(builder.asc(root.get("sequence").as(Integer.class)));
                Predicate[] p = new Predicate[list.size()];
                return builder.and(list.toArray(p));
            }
        };
    }

    public ResultBean modifyAdvertisement(Map map){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            CjCarousel advertisement = cjCarouselRepository.findOne(map.get("id").toString());
            if(advertisement!=null && advertisement.getDelFlag()==0){
                BeanUtils.setBeanData(advertisement, map);
                cjCarouselRepository.saveAndFlush(advertisement);
            }
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    public ResultBean deleteAdvertisement(String id){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            CjCarousel advertisement = cjCarouselRepository.findOne(id);
            if(advertisement!= null){
                advertisement.setDelFlag(1);
            }
            cjCarouselRepository.saveAndFlush(advertisement);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
}
